package com.emp.service.impl;

import java.util.Set;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.emp.bean.User;
import com.emp.dao.UserDao;
import com.emp.service.UserService;
import com.emp.util.SaltUtil;

@Service
@Transactional(rollbackFor=Exception.class)
public class UserServiceImpl implements UserService {
	@Resource
    private UserDao userDao;
	
	@Override
	public void doRegister(String username, String password) throws Exception {
		  //随机生成盐
		  String salt = SaltUtil.generateSalt();
		  //加密密码
		   Md5Hash md5 = new Md5Hash(password, salt, 1);
		  String  md5Pass = md5.toHex();//获得密文(明文+盐加密)
		  User user = new User();
		  user.setUserId(UUID.randomUUID().toString());
		  user.setUsername(username);
		  user.setPassword(md5Pass);
		  user.setSalt(salt);
		  userDao.save(user);
	}

	@Override
	public User queryUserByName(String username) throws Exception {
		User user = userDao.queryUserByName2(username);
		return user;
	}

	@Override
	public Set<String> getRoles(String userName) {
         Set<String> roles = userDao.queryRoles(userName);
		return roles;
	}

	@Override
	public Set<String> getPermissions(String userName) {
	   Set<String> permissions = userDao.queryPermission(userName);
		return permissions;
	}

}
